package cn.itcast.com.homework.day86.Server1;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;

public class Server1 {
    public static void main(String[] args) throws IOException {
        //创建一个socket服务器，端口设置于客户端对应
        ServerSocket serverSocket = new ServerSocket(8282);
        //通过accept方法侦听来自客户端的消息
        Socket socket = serverSocket.accept();

        InputStream in = socket.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(in);

        //创建Properties对象,读取配置文件，读取文件地址
        Properties properties = new Properties();
        properties.load(new InputStreamReader(new FileInputStream("D:\\代码\\代码\\代码\\cn\\itcast\\com\\homework\\day86\\configuration.properties"),"GBK"));
        String dscAddress = properties.getProperty("ImgDscAddress");

        FileOutputStream fos = new FileOutputStream(dscAddress);
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        int len;
        byte[] bytes = new byte[1024];
        while ((len = bis.read(bytes)) != -1){
            bos.write(bytes, 0, len);
        }
        //返还确认消息
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("文件上传完毕".getBytes());

        //释放资源
        bos.close();
        socket.close();
        serverSocket.close();
        bis.close();
    }
}
